package com.kevis.zhunblog.admin.controller.modules.sys;

import com.github.pagehelper.PageInfo;
import com.kevis.webCommon.constant.ControllerConstant;
import com.kevis.webCommon.web.domain.response.Result;
import com.kevis.webCommon.web.domain.response.module.ResultTable;
import com.kevis.webCommon.web.domain.response.module.ResultTree;
import com.kevis.zhunblog.admin.controller.base.BaseController;
import com.kevis.zhunblog.core.dto.PageDomain;
import com.kevis.zhunblog.core.entities.RoleEntity;
import com.kevis.zhunblog.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

@RestController
@Api(tags = {"系统角色"})
@RequestMapping(ControllerConstant.API_SYSTEM_PREFIX + "role")
public class SysRoleController extends BaseController {
    /**
     * Describe: 基础路径
     */
    private static String MODULE_PATH = "system/role/";

    /**
     * Describe: 角色模块服务
     */
    @Autowired
    private RoleService sysRoleService;

    /**
     * Describe: 获取角色列表视图
     * Param ModelAndView
     * Return 用户列表视图
     */
    @GetMapping("main")
    @ApiOperation(value = "获取角色列表视图")
    //@PreAuthorize("hasPermission('/system/role/main','sys:role:main')")
    public ModelAndView main() {
        return jumpPage(MODULE_PATH + "main");
    }

    /**
     * Describe: 获取角色列表数据
     * Param SysRole PageDomain
     * Return 角色列表数据
     */
    @PostMapping("list")
    @ApiOperation(value = "获取角色列表数据")
    //@PreAuthorize("hasPermission('/system/role/data','sys:role:data')")
    public ResultTable data(PageDomain pageDomain, RoleEntity param) {
        PageInfo<RoleEntity> pageInfo = sysRoleService.page(param, pageDomain);
        return pageTable(pageInfo.getList(), pageInfo.getTotal());
    }

    @PostMapping("data")
    @ApiOperation(value = "获取角色列表数据")
    //@PreAuthorize("hasPermission('/system/role/data','sys:role:data')")
    public List data() {
        List<RoleEntity> list = sysRoleService.list();
        return list;
    }

    /**
     * Describe: 获取角色新增视图
     * Param ModelAndView
     * Return 角色新增视图
     */
    @GetMapping("add")
    @ApiOperation(value = "获取角色新增视图")
    //@PreAuthorize("hasPermission('/system/role/add','sys:role:add')")
    public ModelAndView add() {
        return jumpPage(MODULE_PATH + "add");
    }

    /**
     * Describe: 保存角色信息
     * Param SysRole
     * Return 执行结果
     */
    @PostMapping("save")
    @ApiOperation(value = "保存角色数据")
    //@PreAuthorize("hasPermission('/system/role/add','sys:role:add')")
    public Result save(@RequestBody RoleEntity sysRole) {
        //sysRole.setDataScope(Scope.ALL);
        return Result.decide(sysRoleService.save(sysRole));
    }

    /**
     * Describe: 获取角色修改视图
     * Param ModelAndView
     * Return 角色修改视图
     */
    @GetMapping("edit")
    @ApiOperation(value = "获取角色修改视图")
    //@PreAuthorize("hasPermission('/system/role/edit','sys:role:edit')")
    public ModelAndView edit(ModelAndView modelAndView, String roleId) {
        modelAndView.addObject("sysRole", sysRoleService.getById(roleId));
        modelAndView.setViewName(MODULE_PATH + "edit");
        return modelAndView;
    }

    /**
     * Describe: 修改角色信息
     * Param SysRole
     * Return 执行结果
     */
    @PutMapping("edit")
    @ApiOperation(value = "修改角色数据")
    //@PreAuthorize("hasPermission('/system/role/edit','sys:role:edit')")
    public Result update(@RequestBody RoleEntity sysRole) {
        return Result.decide(sysRoleService.update(sysRole));
    }

//    /**
//     * Describe: 根据 Id 启用角色
//     * Param: roleId
//     * Return: Result
//     */
//    @PutMapping("enable")
//    @ApiOperation(value = "启用角色")
//    public Result enable(@RequestBody RoleEntity sysRole) {
//        sysRole.setStatus((byte)1);
//        return decide(sysRoleService.update(sysRole));
//    }
//
//    /**
//     * Describe: 禁用角色
//     * Param: roleId
//     * Return: Result
//     */
//    @PutMapping("disable")
//    @ApiOperation(value = "禁用角色")
//    public Result disable(@RequestBody RoleEntity sysRole) {
//        sysRole.setStatus((byte)0);
//        return decide(sysRoleService.update(sysRole));
//    }

    @PutMapping("enable")
    @ApiOperation(value = "启用/禁用角色")
    public Result setEnable(@RequestBody RoleEntity entity) {
        //public Result updateStatus(Integer userId, Integer enable) {
        return Result.decide(sysRoleService.updateById(entity));
    }

//    /**
//     * Describe: 用户删除接口
//     * Param: id
//     * Return: Result
//     */
//    @DeleteMapping("remove/{id}")
//    @ApiOperation(value = "删除角色数据")
//    //@PreAuthorize("hasPermission('/system/role/remove','sys:role:remove')")
//    public Result remove(@PathVariable Integer id) {
//        return decide(sysRoleService.remove(id));
//    }

    /**
     * Describe: 用户删除接口
     * Param: id
     * Return: Result
     */
    @DeleteMapping("remove")
    @ApiOperation(value = "删除角色数据")
    //@PreAuthorize("hasPermission('/system/role/remove','sys:role:remove')")
    public Result del(Integer id) {
        return Result.decide(sysRoleService.remove(id));
    }

    /**
     * Describe: 用户批量删除接口
     * Param: ids
     * Return: Result
     */
//    @DeleteMapping("batchRemove/{ids}")
//    @ApiOperation(value = "批量删除角色数据")
//    //@PreAuthorize("hasPermission('/system/role/remove','sys:role:remove')")
//    public Result batchRemove(@PathVariable String ids) {
//        return decide(sysRoleService.batchRemove(ids.split(",")));
//    }

    /**
     * Describe: 获取角色授权视图
     * Param ModelAndView
     * Return ModelAndView
     */
    @GetMapping("power")
    @ApiOperation(value = "获取分配角色权限视图")
    //@PreAuthorize("hasPermission('/system/role/power','sys:role:power')")
    public ModelAndView power(Model model, Integer roleId) {
        model.addAttribute("roleId", roleId);
        return jumpPage(MODULE_PATH + "power");
    }

    /**
     * Describe: 获取角色权限
     * Param RoleId
     * Return ResultTree
     */
    @GetMapping("getRolePower")
    @ApiOperation(value = "获取角色权限数据")
    //@PreAuthorize("hasPermission('/system/role/power','sys:role:power')")
    public ResultTree getRolePower(Integer roleId) {
        return dataTreeObject(sysRoleService.getRolePower(roleId));
    }

    /**
     * Describe: 保存操作权限
     * Param RoleId PowerIds
     * Return Result
     */
    @PutMapping("saveRolePower")
    @ApiOperation(value = "保存角色权限数据")
    //@PreAuthorize("hasPermission('/system/role/power','sys:role:power')")
    public Result saveRolePower(Integer roleId, String powerIds) {
        return Result.decide(sysRoleService.saveRolePower(roleId, Arrays.asList(powerIds.split(","))));
    }

}
